package com.liangtian.mdm.dm.jsp.service.imp;

import com.liangtian.mdm.base.dao.JdbcDao;
import com.liangtian.mdm.dm.config.dao.AttrConfigDao;
import com.liangtian.mdm.dm.config.model.AttrConfig;
import com.liangtian.mdm.dm.jsp.service.BootstrapService;
import org.apache.commons.lang3.ObjectUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@SuppressWarnings({"rawtypes", "unchecked"})
@Service
public class BootstrapServiceImpl implements BootstrapService {

    @Autowired
    private AttrConfigDao attrConfigDao;
    //	@Autowired
//	private JspActionDao jspActionDao;
    @Autowired
    private JdbcDao jdbcDao;

    @Override
    public Map<String, Object> queryBootSel(String table, String code) {
        Map<String, Object> retMap = new HashMap<>();
//		Object obj = new Object();
        if (StringUtils.isEmpty(code) || StringUtils.isEmpty(table)) {
            System.out.println("传递过来的参数有问题...");
        } else {
            try {
                Map<String, String> map = new HashMap<String, String>();
                map.put("attrColumn", code.toUpperCase());
                Map<String, String> queryMap = new HashMap<String, String>();
                if (table.startsWith("T_") || table.startsWith("t_")) {//表示当前数据是主数据
                    map.put("modelId", table.substring(2, table.length()));
                    AttrConfig attrConfig = attrConfigDao.queryAttrInfoByAttrColumn(map);
                    if (!ObjectUtils.isEmpty(attrConfig)) {//如果查询结果不为空
                        String dsTable = attrConfig.getDataSourceTable();
                        String dsColumn = attrConfig.getDataSourceShowColumn();

                        queryMap.put("queTable", "T_" + dsTable);
                        queryMap.put("column", " id||',,,'||" + dsColumn);
                        queryMap.put("col", dsColumn);
                    }
                } else {
                    queryMap.put("queTable", table);
                    queryMap.put("column", "distinct " + isAcronym(code));
//					queryMap.put("column", "distinct "+code);
                    queryMap.put("col", isAcronym(code));
                }
//				if(StringUtil./)
                List<String> ls = attrConfigDao.queryColumnForSelOption(queryMap);
                if (table.startsWith("T_") || table.startsWith("t_")) {
                    Map<String, String> map1 = new HashMap<String, String>();
                    for (String bs : ls) {
                        map1.put(bs.split(",,,")[1], bs.split(",")[0]);
                    }
                    retMap.put("state", "1");
                    retMap.put("datas", map1);
                } else {
                    retMap.put("state", "0");
                    retMap.put("datas", ls);
                }
            } catch (Exception e) {
                retMap.put("state", "2");
            }

        }
        return retMap;
    }

    public static String isAcronym(String word) {
        String str = "";
        for (int i = 0; i < word.length(); i++) {
            char c = word.charAt(i);
            if (Character.isUpperCase(c)) {
                str += "_";
            }
            str += c;
        }
        return str;
    }

    @Override
    public boolean checkTableValiable(String tableName) {
        String sql = "select count(1) from tabs where table_name ='" + tableName + "'";
        boolean b = jdbcDao.executeSysSql(sql);
        return b;
    }

    @Override
    public List<Map<String, String>> getColAndComment(String table) {
//		List<Map<String,String>> list = new ArrayList<Map<String,String>>();
        String sql = "select column_name code, comments name from user_col_comments where Table_Name = 'AM_MODEL'";
        Map map = jdbcDao.executeSysSqlForComments(sql);
        System.out.println(map);
        return null;
    }
}
